package view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import controller.Main;

public class ContactTable extends JPanel {
	private boolean DEBUG = false;
	private static JTable table;
	public JScrollPane scrollPane;
	String newline = "\n";
	JTextArea output;
	
	public ContactTable(){
	
		
		//super(new GridLayout(1,0));
		String[] columnNames = {"First Name",
								"Last Name",
								"Home Address",
								"Company Address",
								"Personal Phone",
								"Home Page",
								"Email",
								"Photo",
								"Group"};
		final Object [][] data = new Object[Main.tab.size()][columnNames.length];
				for(int i=0; i < Main.tab.size();i++){
					
						data[i][0]=Main.tab.get(i).getFirstName();
						data[i][1]=Main.tab.get(i).getLastName();
						data[i][2]=Main.tab.get(i).getHomeAddress();
						data[i][3]=Main.tab.get(i).getCompanyAddress();
						data[i][4]=Main.tab.get(i).getPersonalPhone();
						data[i][5]=Main.tab.get(i).getHomePage();
						data[i][6]=Main.tab.get(i).getEmail();
						data[i][7]=Main.tab.get(i).getPhoto();
						data[i][8]=Main.tab.get(i).getGroup();
						
					
				}
				
		this.setTable(new JTable(data, columnNames));
	    getTable().setPreferredScrollableViewportSize(new Dimension(800, 200));
	    getTable().setFillsViewportHeight(true);
	    getTable().setBounds(0,0,100,100);
	    getTable().setAutoCreateRowSorter(true);

	    
	    if (DEBUG) {
            getTable().addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(getTable());
                }
            });
        }

        //Create the scroll pane and add the table to it.
	    scrollPane= new JScrollPane(getTable());
        
        //Add the scroll pane to this panel.
        add(scrollPane);
        
        //action listener
       getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        	public void valueChanged(ListSelectionEvent event){
        		
        		int i = getTable().getSelectedRowCount();
        		
        		ContactFrame.initScreen(i,
        								data[i][0].toString(),
        								data[i][1].toString(),
        								data[i][2].toString(),
        								data[i][3].toString(),
        								data[i][4].toString(),
        								data[i][5].toString(),
        								data[i][6].toString(),
        								data[i][7].toString(),
        								data[i][8].toString());

        	}
        	
        });
       
        
        
    }
/*
	
	public class SharedListSelectionHandler implements ListSelectionListener {

		
		public void valueChanged(ListSelectionEvent e) {
	        ListSelectionModel lsm = (ListSelectionModel)e.getSource();

	        int firstIndex = e.getFirstIndex();
	        int lastIndex = e.getLastIndex();
	        boolean isAdjusting = e.getValueIsAdjusting();
			output.append("Name ");

	        if (lsm.isSelectionEmpty()) {
	            output.append(" <none>");
	        } else {
	            // Find out which indexes are selected.
	            int minIndex = lsm.getMinSelectionIndex();
	            int maxIndex = lsm.getMaxSelectionIndex();
	            for (int i = minIndex; i <= maxIndex; i++) {
	                if (lsm.isSelectedIndex(i)) {
	                    output.append(" " + i);
	                }
	            }
	        }
	        output.append(newline);
	        output.setCaretPosition(output.getDocument().getLength());
	    }

	} 
		*/

	
	

private void printDebugData(JTable table) {
    int numRows = table.getRowCount();
    int numCols = table.getColumnCount();
    javax.swing.table.TableModel model = table.getModel();

    System.out.println("Value of data: ");
    for (int i=0; i < numRows; i++) {
        System.out.print("    row " + i + ":");
        for (int j=0; j < numCols; j++) {
            System.out.print("  " + model.getValueAt(i, j));
        }
        System.out.println();
    }
    System.out.println("--------------------------");
}

public static JTable getTable() {
	return table;
}

public void setTable(JTable table) {
	this.table = table;
}
	
	}


